package com.aaa.controller.front;

import com.aaa.entity.FrontUserinfo;
import com.aaa.entity.Goods;
import com.aaa.entity.ShoppingCart;
import com.aaa.entity.ShoppingCartDetail;
import com.aaa.service.front.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/front")
@Controller
public class CartListController {
    @Resource
    private ZMRIGoodsService zmrGoodsService;
    @Resource
    private IShoppingCartDetailService shoppingCartDetailService;
    @Resource
    private IShoppingCartService shoppingCartService;



    @RequestMapping("/cart_add")
    public String cart_add(String goodsId,HttpSession session,Double retailPrice){
        FrontUserinfo userInfo =(FrontUserinfo) session.getAttribute("userinfo");
        ShoppingCart cart = shoppingCartService.findCartUserInfoId(userInfo.getUserInfoId());


        if (cart==null){
            //如果为空，在购物车中加入该用户购物车
            ShoppingCart shoppingCart=new ShoppingCart();
            shoppingCart.setUserInfoId(userInfo.getUserInfoId());
            shoppingCart.setShoppingCartId(shoppingCart.getShoppingCartId());
            shoppingCartService.insertCart(shoppingCart);


            //将该购物车加入购物车明细表
            ShoppingCart shoppingCart2 = shoppingCartService.findCartUserInfoId(userInfo.getUserInfoId());
            ShoppingCartDetail cartDetail=new ShoppingCartDetail();
            cartDetail.setShoppingCartId(shoppingCart2.getShoppingCartId());
            cartDetail.setGoodsId(goodsId);
            cartDetail.setGoodsCount(1);
            cartDetail.setGoodsAmount(retailPrice);
            shoppingCartDetailService.insertGoods(cartDetail);
        }else {

            //判断购物车明细中是否有该商品
            ShoppingCartDetail c = shoppingCartDetailService.findShoppingCartDetailByGoodsIdAndShoppingCartId(goodsId,cart.getShoppingCartId());
            System.out.println(c);
            if(c==null){
                //没有：加入购物车
                ShoppingCartDetail cartDetail=new ShoppingCartDetail();
                cartDetail.setShoppingCartId(cart.getShoppingCartId());
                cartDetail.setGoodsId(goodsId);
                cartDetail.setGoodsCount(1);
                cartDetail.setGoodsAmount(retailPrice);
                shoppingCartDetailService.insertGoods(cartDetail);
            }else{
                //有：数量加一
                shoppingCartDetailService.updateGoodsNum(goodsId,c.getShoppingCartId(),1);
            }
        }
        return "redirect:/front/shop-details-variable?goodsId="+goodsId;
    }


    @RequestMapping("/cart_process")
    public String cart_process(Integer UserInfoId,String goodsId,String op,HttpSession session){
        FrontUserinfo userInfo =(FrontUserinfo) session.getAttribute("userinfo");
        ShoppingCart cart = shoppingCartService.findCartUserInfoId(userInfo.getUserInfoId());
        ShoppingCartDetail cartDetail = shoppingCartDetailService.findShoppingCartDetailByGoodsIdAndShoppingCartId(goodsId,cart.getShoppingCartId());
        if ("add".equals(op)){
            shoppingCartDetailService.updateGoodsNum(goodsId,cart.getShoppingCartId(),cartDetail.getGoodsCount()+1);
        }else if ("sub".equals(op)){
            if (cartDetail.getGoodsCount()==1){
              shoppingCartDetailService.deleteGoodsDetail(cart.getShoppingCartId(),goodsId);
            }
            shoppingCartDetailService.updateGoodsNum(goodsId,cart.getShoppingCartId(),cartDetail.getGoodsCount()-1);
        }else if ("delete".equals(op)){
            shoppingCartDetailService.deleteGoodsDetail(cart.getShoppingCartId(),goodsId);

        }

        return "redirect:/front/cart_show?UserInfoId="+userInfo.getUserInfoId();
    }

    /**
     * 显示购物车页面
     * @param
     * @param model
     * @return
     */
    @RequestMapping("/cart_show")
    public String cart_show(HttpSession session,Model model){
        FrontUserinfo userinfo = (FrontUserinfo) session.getAttribute("userinfo");
        Integer UserInfoId=userinfo.getUserInfoId();
        ShoppingCart cart = shoppingCartService.findCartUserInfoId(UserInfoId);
        List<ShoppingCartDetail> userInfoCartDetail = shoppingCartDetailService.findShoppingCartDetail(cart.getShoppingCartId());
        int totalCount=0;
        double totalAmount=0;
        for (ShoppingCartDetail g:userInfoCartDetail){
            totalCount +=  g.getGoodsCount();
            totalAmount+=g.getGoodsAmount()*g.getGoodsCount();
        }
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("totalAmount",totalAmount);
        session.setAttribute("totalAmount",totalAmount);
        model.addAttribute("userInfoCartDetail",userInfoCartDetail);
       System.out.print(userInfoCartDetail);
        return "/front/cart-list";
    }

    /**
     * 订单
     * @param model
     * @param userInfoId
     * @return
     */
    /**
    @RequestMapping("/order")
    public String cartlist(Model model, Integer userInfoId){
        List<Goods> goods = shoppingCartDetailService.findGoodsByUserId(userInfoId);
        ShoppingCart cart = shoppingCartService.findCartUserInfoId(userInfoId);
        List<ShoppingCartDetail> userInfoCartDetail = shoppingCartDetailService.findShoppingCartDetail(cart.getShoppingCartId());
        int totalCount=0;
        double totalAmount=0;
        for (ShoppingCartDetail g:userInfoCartDetail){
            totalCount +=  g.getGoodsCount();
            totalAmount += g.getGoodsAmount();
        }
        return "/front/cart-list";
    }
*/
}
